﻿using SearchAlgorithms.Algorithms.Search;
using SearchAlgorithms.extension;
using System;
using System.Collections.Generic;
using System.Threading.Tasks.Dataflow;

namespace SearchAlgorithms.Services
{
    public class SegmentBlock
    {
        private readonly byte[] _pattern;
        private int _LastbufferSize;
        private string _algorithmName;
        private List<byte> LastBuffer = new List<byte>(); // 缓存区，存储未处理的数据
        private int flag = 1;       //新一帧的起始标志

        public SegmentBlock(byte[] pattern, int bufferSize, string method)
        {
            _pattern = (byte[])pattern.Clone();
            _LastbufferSize = bufferSize;
            _algorithmName = method;

            _Process = new ActionBlock<byte[]>(buffer =>
            {
                LastBuffer.AddRange(buffer);

                while (LastBuffer.ToArray().Length > _pattern.Length)
                {
                    var temp = LastBuffer.ToArray().AsSpan();

                    int result = 0;
                    if (_algorithmName == "IndexOf")
                    {
                        result = temp.IndexOf(_pattern); //用搜索算法搜素帧头
                    }
                    else if (_algorithmName == "Rubiksmaster02")
                    {
                        result = temp.SingleSearch(_pattern, Rubiksmaster02.Default); //用搜索算法搜素帧头
                    }
                    else if (_algorithmName == "KMP")
                    {
                        result = temp.SingleSearch(_pattern, KMPSearch.Default); //用搜索算法搜素帧头
                    }
                    else if (_algorithmName == "BruteSearch")
                    {
                        result = temp.SingleSearch(_pattern, Brutesearch.Default); //用搜索算法搜素帧头
                    }
                    else
                    {
                        Console.WriteLine("该方法不存在，已采用默认方法");
                        result = temp.IndexOf(_pattern); //用搜索算法搜素帧头
                    }

                    if (result != -1)
                    {
                        flag = 1;
                        LastBuffer.RemoveRange(0, result + _pattern.Length);
                        DataArrived?.Invoke((temp[0..result].ToArray(), flag));
                    }

                    else
                    {
                        flag = 0;
                        LastBuffer.RemoveRange(0, (temp.Length - _pattern.Length + 1));    //去除安全区，留下怀疑区
                        DataArrived?.Invoke((temp[0..(temp.Length - _pattern.Length + 1)].ToArray(), flag));

                    }
                }
                
                //最后一帧的处理
                if (buffer.Length < _LastbufferSize)
                {
                    DataArrived?.Invoke((LastBuffer.ToArray(), 1));
                    LastBuffer.Clear();
                    
                }
            });
        }

        

        public void Enqueue(byte[] buffer)
        {
            _Process.Post(buffer);//向分帧模块传入从文件读取的数据块
        }
        
        public ActionBlock<byte[]> _Process;
        
        public Action<(byte[], int)> DataArrived;
    }
}






